const db = uniCloud.database()
const dbCmd = db.command
const {
	zmfRecordInfoCollection
} = require('../../../common/constants')
const {
	ERROR
} = require('../../../common/error')

/**
 * 新增操作记录信息
 * @param {Object}  params
 * @param {String}  params.uid     			用户id
 * @param {String}  params.type     		积分类型 1:语音合成 2:试听记录
 * @param {String}  params.state     		状态 
 * @param {String}  params.text     		文本内容
 * @param {String}  params.html     		html
 * @param {String}  params.url     			生成路径
 * @param {String}  params.use_words     	使用字符数
 * @param {String}  params.create_date     	创建时间
 * @param {String}  params.expire_date     	过期时间
 * @param {String}  params.sessionId		语音合成返回
 * @param {String}  params.subtitles		语音合成返回
 * 
 */
module.exports = async function(params = {}) {
	const schema = {
		uid: 'string',
		type: {
			required: false,
			type: 'number'
		},
		state: {
			required: false,
			type: 'number'
		},
		text: {
			required: false,
			type: 'string'
		},
		html: {
			required: false,
			type: 'string'
		},
		url: {
			required: false,
			type: 'string'
		},
		use_words: {
			required: false,
			type: 'number'
		},
		create_date: {
		  required: true,
		  type: 'number'
		},
		expire_date: {
		  required: true,
		  type: 'number'
		},
		sessionId: {
		  required: false,
		  type: 'string'
		},
		subtitles: {
		  required: false,
		  type: 'array'
		},
	}
	this.middleware.validate(params, schema)
	const {
		uid,
		type,
		state,
		text,
		html,
		url,
		use_words,
		create_date,
		expire_date,
		sessionId,
		subtitles
	} = params
	
	// 数据
	const data = {
		uid,
		type,
		state,
		text,
		html,
		url,
		use_words,
		create_date,
		expire_date,
		sessionId,
		subtitles
	}

	const resp = await zmfRecordInfoCollection.add(data)
	return {
	  errCode: 0,
	  userRecord: resp
	}
}